package queue;
import MyOs.*;
import exception.*;
public class linkqueue implements queue{
    private node front;
    private node rear;
    private static node getone;
    private static node getPid;
    private static node getStatus;
    
    public linkqueue(){
        getone=front;
        getPid=front;
        getStatus=front;
        this.front=this.rear=null;
    }
    
    public void clear(){
        this.front=this.rear=null;
    }
    
    public boolean isEmpty(){
        return this.front==null;
    }
    
    public Pcb getFront(){
        if(this.isEmpty()==true)
            throw new IllegalArgumentException("队已经为空");
        return this.front.data;
    }
    public void enQueue(int o){
        node a=new node(o);
        if(this.front==null){
            a.next=this.front;
            this.front=this.rear=a;
        }
        else{
            node pos=this.rear;
            while(pos.next!=null)
                pos=pos.next;
            a.next=pos.next;
            pos.next=a;
        }
    }
    
    public Pcb deQueue(){
        if(this.isEmpty()==true)
            throw new IllegalArgumentException("队已经为空");
        node pos=this.front;
        front=front.next;
        rear=front;
        return  pos.data;
    }
    
    public Pcb delete(int id){
        this.choose(id);//检验是否存在该id的pcb
        if(this.isEmpty()==true)
            throw new IllegalArgumentException("队已经为空");
        node pos=this.front;
    	Pcb a=null;
        if(pos.data.pid==id){
            front=rear=pos.next;
            return pos.data;
        }     
        while(pos.data.pid!=id){
            node prior=pos;//创建prior来保存pos的位置
            pos=pos.next;
            if(pos.data.pid==id){
               if(pos.next==null){
                   prior.next=null;
                   a=pos.data;
                   break;
               } 
               else{
                   prior.next=pos.next;
                   a=pos.data;
                   break;
               }
            }
        }
        return a;
    }
    
    public  Pcb choose(int id){
        if(null==this.front)
            throw new IllegalArgumentException("该队列为空,无法进行选择操作");
        node pos=this.front;
        while(pos.data.getPid()!=id&&pos!=null){
            pos=pos.next;
            if(pos!=null&&pos.data.getPid()==id)
                break;
            if(null==pos)
                throw new IllegalArgumentException("无法匹配到id为"+id+"的进程");
        }
        return pos.data;
    }
    
    public Pcb getOnePcb(){
        if(this.isEmpty()==true)
            throw new EmptyCollectionException("queue");
        if(null==getone)
            getone=this.front;
        Pcb pb=getone.data;
        getone=getone.next;
        return pb;
    }
    
    public int getSize(){
        node pos=this.front;
        int count=0;
        if(null==pos)
            return 0;
        while(pos!=null){
            count++;
            pos=pos.next;
        }
        return count;
    }
    
    public int getTraversePid(){
        if(null==getPid)
            getPid=this.front;
        if(getPid==null)
            throw new EmptyCollectionException("queue");
        int i=getPid.data.getPid();
        getPid=getPid.next;
        return i;
    }
    
    /*public String getTraverseStatus(){
        if(null==getStatus)
            getStatus=this.front;
        if(getStatus==null)
            throw new EmptyCollectionException("queue");
        Status info=getStatus.data.getStatus();
        String result=null;
        switch(info){
            case RUNNING:result="RUNNING";break;
            case READY:result="READY";break;
            case WAITING:result="WAITING";break;
        }
        getStatus=getStatus.next;
        return result;
    }*/
    
    public void backone(){
        getPid=null;
        getStatus=null;
    }
    
    public void backtwo(){
        getone=null;
    }
}
